// JavaScript Document

_CROP_PLANT = 0;
_FLOWER_PLANT = 1;
_TREE_PLANT = 2;

_dummy_plant_id = 'plant_1006';
_dummy_food_id = 'food_0000';

var json_init_callback;

function callback_json (data){
alert(data);
	if(json_init_callback)
		json_init_callback(data);
}

function twitHack() {
	//var tmp_json_data;
	this.user_id = user_id;
	this.data_type = "";
	var callback;

	this.url = "http://125.6.168.204/playphone/mb002/json_output/user_data.php";

	var self = this;
	this.get_json_data = function (url, action, parameter){
		var url_ku = (parameter != null) ? url+"?action="+action+"&jsoncallback=?&"+parameter : url+"?action="+action+"&jsoncallback=?";
		$.getJSON(url_ku,  callback_json);
	}
}


SendData.prototype = {

	userInitialization: function(then){
		json_init_callback = then;
		this.data_type = 'user_init';
		this.get_json_data(this.url, "init", "user_id="+this.user_id);
	},

	getUserPlantList: function(then, certain_id){
		json_init_callback = then;
		this.data_type = 'user_plant_list';
		if(certain_id)
			var parameter = "user_id="+this.user_id+"&certain_id="+certain_id;
		else
			var parameter = "user_id="+this.user_id;
		this.get_json_data(this.url, "user_plant_list", parameter);
	},

	getUserAnimalList: function(then, certain_id){
		json_init_callback = then;
		this.data_type = 'user_animal_list';
		if(certain_id)
			var parameter = "user_id="+this.user_id+"&certain_id="+certain_id;
		else
			var parameter = "user_id="+this.user_id;
		this.get_json_data(this.url, "user_animal_list", parameter);
	},

	getUserFoodList: function(then, certain_id){
		json_init_callback = then;
		this.data_type = 'user_food_list';
		if(certain_id)
			var parameter = "user_id="+this.user_id+"&certain_id="+certain_id;
		else
			var parameter = "user_id="+this.user_id;
		this.get_json_data(this.url, "user_food_list", parameter);
	},

	getPlantInventoryN : function(then){
		json_init_callback = then;
		this.data_type = 'plant_inventory_num';
		this.get_json_data(this.url, "plant_inventory_num", "user_id="+this.user_id);
	},

	getFoodInventoryN : function(then){
		json_init_callback = then;
		this.data_type = 'food_inventory_num';
		this.get_json_data(this.url, "food_inventory_num", "user_id="+this.user_id);
	},

	wateringAllSoil: function(target_id, then){
		json_init_callback = then;
		this.data_type = 'water';
		this.get_json_data(this.url, "update_water&target_id="+target_id+"&user_id="+this.user_id);
	},

	strokingAllAnimal : function(target_id, then){
		json_init_callback = then;
		this.data_type = 'stroke';
		this.get_json_data(this.url, "update_stroke&target_id="+target_id+"&user_id="+this.user_id);
	},

	updateLive : function(then){
		json_init_callback = then;
		this.data_type = 'lifecycle';
		this.get_json_data(this.url, "update_lifecyle&user_id="+this.user_id);
	},

	updateGrowth : function(then){
		json_init_callback = then;
		this.data_type = 'growth';
		this.get_json_data(this.url, "update_growth&user_id="+this.user_id);
	},

	/**
	 * @parameter : seed_arr is a colon separated array that contains array of cell_no
	 *  seed_arr = ["<cell_no>,<cell_no>,<cell_no>"] and so on
	**/
	seedPlant: function(selected_plant_id, seed_arr, soil_type, then){
		json_init_callback = then;
		this.data_type = 'seeding';
		_dummy_plant_id = selected_plant_id;
		var plant_id = _dummy_plant_id; // dummy
		this.get_json_data(this.url, "seeding", "user_id="+this.user_id+"&seeded_arr="+seed_arr.join(";")+"&soil_type="+soil_type+"&plant_id="+plant_id);
	},

	/**
	 * @parameter : feed_arr is a colon separated array that contains array of cell_no
	 *  feed_arr = ["<cell_no>,<cell_no>,<cell_no>"] and so on
	**/
	feedAnimal: function(feed_arr, then){
		json_init_callback = then;
		this.data_type = 'feeding';
		var food_id = _dummy_food_id; // dummy
		this.get_json_data(this.url, "feeding", "user_id="+this.user_id+"&feed_arr="+feed_arr.join(";")+"&food_id="+food_id);
	},

	/**
	 * make an union string containing [plant_uno,plant_id,grow_level,helped_count,series_count
	 * from the existent plant and animal array
	 **/
	harvest: function(plant_arr, tree_arr, animal_arr, then){
		// plant
		var plant_uno = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_uno += plant_arr[i]['uno'];
			else
				plant_uno += ";"+plant_arr[i]['uno'];
		}
		var plant_id = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_id += plant_arr[i]['plant_id'];
			else
				plant_id += ";"+plant_arr[i]['plant_id'];
		}
		var plant_grow_level = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_grow_level += plant_arr[i]['grow_level'];
			else
				plant_grow_level += ";"+plant_arr[i]['grow_level'];
		}
		var plant_helped_count = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_helped_count += plant_arr[i]['helped_count'];
			else
				plant_helped_count += ";"+plant_arr[i]['helped_count'];
		}
		var plant_series_count = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_series_count += plant_arr[i]['series_count'];
			else
				plant_series_count += ";"+plant_arr[i]['series_count'];
		}
		var plant_cell_no = "";
		for(var i=0;i<plant_arr.length;i++){
			if(i==0)
				plant_cell_no += plant_arr[i]['cell_no'];
			else
				plant_cell_no += ";"+plant_arr[i]['cell_no'];
		}

		var plant_union = plant_uno+","+plant_id+","+plant_grow_level+","+plant_helped_count+","+plant_series_count+","+plant_cell_no;

		// tree
		var tree_uno = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_uno += tree_arr[i]['uno'];
			else
				tree_uno += ";"+tree_arr[i]['uno'];
		}
		var tree_id = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_id += tree_arr[i]['plant_id'];
			else
				tree_id += ";"+tree_arr[i]['plant_id'];
		}
		var tree_grow_level = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_grow_level += tree_arr[i]['grow_level'];
			else
				tree_grow_level += ";"+tree_arr[i]['grow_level'];
		}
		var tree_helped_count = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_helped_count += tree_arr[i]['helped_count'];
			else
				tree_helped_count += ";"+tree_arr[i]['helped_count'];
		}
		var tree_series_count = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_series_count += tree_arr[i]['series_count'];
			else
				tree_series_count += ";"+tree_arr[i]['series_count'];
		}
		var tree_cell_no = "";
		for(var i=0;i<tree_arr.length;i++){
			if(i==0)
				tree_cell_no += tree_arr[i]['cell_no'];
			else
				tree_cell_no += ";"+tree_arr[i]['cell_no'];
		}

		var tree_union = tree_uno+","+tree_id+","+tree_grow_level+","+tree_helped_count+","+tree_series_count+","+tree_cell_no;

		//animal
		var animal_uno = "";
		for(var i=0;i<animal_arr.length;i++){
			if(i==0)
				animal_uno += animal_arr[i]['uno'];
			else
				animal_uno += ";"+animal_arr[i]['uno'];
		}
		var animal_id = "";
		for(var i=0;i<animal_arr.length;i++){
			if(i==0)
				animal_id += animal_arr[i]['animal_id'];
			else
				animal_id += ";"+animal_arr[i]['animal_id'];
		}
		var animal_grow_level = "";
		for(var i=0;i<animal_arr.length;i++){
			if(i==0)
				animal_grow_level += animal_arr[i]['grow_level'];
			else
				animal_grow_level += ";"+animal_arr[i]['grow_level'];
		}
		var animal_helped_count = "";
		for(var i=0;i<animal_arr.length;i++){
			if(i==0)
				animal_helped_count += animal_arr[i]['helped_count'];
			else
				animal_helped_count += ";"+animal_arr[i]['helped_count'];
		}
		var animal_cell_no = "";
		for(var i=0;i<animal_arr.length;i++){
			if(i==0)
				animal_cell_no += animal_arr[i]['cell_no'];
			else
				animal_cell_no += ";"+animal_arr[i]['cell_no'];
		}
		var animal_union = animal_uno+","+animal_id+","+animal_grow_level+","+animal_helped_count+",,"+animal_cell_no;


		json_init_callback = then;
		this.data_type = 'harvest';
		this.get_json_data(this.url, "harvest", "user_id="+this.user_id+"&plant_union="+plant_union+"&tree_union="+tree_union+"&animal_union="+animal_union);
	},

	buy_plant : function(plant_id, num, price, then){
		json_init_callback = then;
		this.data_type = 'buy_plant';
		this.get_json_data(this.url, "buy_plant", "user_id="+this.user_id+"&plant_id="+plant_id+"&num="+num+"&price="+price);
	},

	buy_animal : function(animal_id, num, price, then){
		json_init_callback = then;
		this.data_type = 'buy_animal';
		this.get_json_data(this.url, "buy_animal", "user_id="+this.user_id+"&animal_id="+animal_id+"&num="+num+"&price="+price);
	},

	buy_food : function(food_id, num, price, then){
		json_init_callback = then;
		this.data_type = 'buy_food';
		this.get_json_data(this.url, "buy_food", "user_id="+this.user_id+"&food_id="+food_id+"&num="+num+"&price="+price);
	},

	callHelp : function(then, is_call_help){
		json_init_callback = then;
		this.data_type = 'call_help';
		var call_help_str = "no";
		if(is_call_help) call_help_str = "yes";
		this.get_json_data(this.url, "call_help", "user_id="+this.user_id+"&call_help="+call_help_str);
	},

	get_accident_status : function(then){
		json_init_callback = then;
		this.data_type = 'get_accident_status';
		this.get_json_data(this.url, "get_accident_status", "user_id="+this.user_id);
	}
}
